package student;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class studentLand {
	private static String use=null;
	public static void land() {
		// TODO Auto-generated method stub
		Scanner sc=new Scanner(System.in);
		System.out.print("请输入用户名：");
		use=sc.nextLine();
		
		System.out.print("请输入密码：");
		String paw=sc.next();
		
		//连接数据库
		Connection conn=null;
		Statement stat=null;
		ResultSet re=null;
		try {
			String url="jdbc:oracle:thin:@localhost:1521:orcl11";
			String user="hr";
			String pas="oracle";
			Class.forName("oracle.jdbc.OracleDriver");
			conn=DriverManager.getConnection(url, user, pas);
			stat=conn.createStatement();
			//获取数据库中的数据
			
			String sql="select stu_pass from student where stu_id="+use;
			re=stat.executeQuery(sql);
			String pw=null;
			while(re.next()) {
				pw=re.getString(1);
				
			}
			if(paw.equals(pw)) {
				if(pw.equals("123456")){
					System.out.println("请修改密码");
					System.out.println("请输入你要修改的密码");
					String ps=sc.next();
					sql="update  student set stu_pass='"+ps+"' where stu_id="+use;
					stat.executeUpdate(sql);
					
				}
				
				three:while(true) {
					System.out.println("**********************************");
					System.out.println("*             1.查询个人信息");
					System.out.println("*             2.修改密码");
					System.out.println("*             3.查询同学信息");
					System.out.println("*             4.添加好友");
					System.out.println("*             5.显示我的所有好友信息");
					System.out.println("*             6.保存好友信息");
					System.out.println("*             7.聊天室");
					System.out.println("*             8.给好友留言");
					System.out.println("*             9.查看我的留言板");
					System.out.println("*             10.删除好友");
					System.out.println("*             11.修改好友备注");
					System.out.println("*             0......");

					System.out.println("请选择功能菜单");

					System.out.println("**********************************");
					
					int fun=sc.nextInt();
					
					switch(fun) {
					case 1:
						//查看个人信息
						sql="select * from student where stu_id="+use;
						re=stat.executeQuery(sql);
						while(re.next()) {
							System.out.println("编号:"+ re.getObject(1)+" 姓名:"+re.getObject(3)+" 性别:"+re.getObject(4)+" 年龄:"+re.getObject(5)+" 电话:"+re.getObject(6)+" 地址:"+re.getObject(7));
						}
						break;
					case 2:
						//修改密码
						System.out.println("请输入你的旧密码");
						String jpw=sc.next();
						
						//判断数据库中的密码跟输入是否相同，相同则继续修改
						sql="select stu_pass from student where stu_id="+use;
						
						while(re.next()) {
							pw=re.getString(1);
						}
						if(pw.equals(jpw)) {
							System.out.println("请输入新密码");
							String xpw=sc.next();
							System.out.println("请重新输入新密码");
							String xxpw=sc.next();
							if(xpw.equals(xxpw)){
							sql="update  student set stu_pass='"+xpw+"' where stu_id="+use;
							stat.executeUpdate(sql);
							System.out.println("密码修改成功");
							}else {
								System.out.println("两次密码不同，更改失败");
							}
							
						}else {
							System.out.println("旧密码输入错误");
						}
						break;
					case 3:
						//查询同学信息
						query();
						break ;
					case 4:
						addFriends(use,stat);
						break ;
					case 5:
						//显示我的所有好友信息
						myFriends(use,stat);
						break ;
					case 6:
						friendInformation(use,stat);
						
						break ;
					case 7:
						LeavingAMessage();
						
						break ;
					case 8:
						
						
						break ;
					case 9:
			
			
						break ;
					case 10:
			
			
						break ;
					case 11:
						
						
						break ;
							
						default:
							break three;
					}
					
					}
				
			}else{System.out.println("账号或密码有误，请重新输入");}
			
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
	}
	
	
	private static void query() {
		Connection conn=null;
		Statement sta=null;
		ResultSet re=null;
		String url="jdbc:oracle:thin:@localhost:1521:orcl11";
		String user="hr";
		String pass="oracle";
		
		System.out.println("请选择查询的功能");
		System.out.println("1.按姓名查询");
		System.out.println("2.性别查询");
		System.out.println("3.按年龄段查询");
		System.out.println("4.按编号查询");
		System.out.println("5.按地址查询");
		System.out.println("6.查询全部");
		System.out.println("7.返回上一级");
		Scanner sc=new Scanner(System.in);
		int fun=sc.nextInt();
		String l=sc.nextLine();
		String sql=null;
		three:while(true) {
		switch(fun) {
		case 1:
			System.out.println("请输入你要查询的关键字");
			String st=sc.nextLine();
			sql="select * from student where stu_name like '%"+st+"%'";
			break three;
		case 2:
			System.out.println("请输入你要查询的关键字");
			st=sc.nextLine();
			sql="select * from student where stu_sex like '%"+st+"%'";
			break three;
		case 3:
			System.out.println("请输入你要查询的年龄的范围,用-隔开");
			st=sc.nextLine();
			String[] array=st.split("-");
			int first=Integer.parseInt(array[0]);
			int lest=Integer.parseInt(array[1]);
			sql="select * from student where stu_age between "+first+" and "+lest;
			break three;
		case 4:
			System.out.println("请输入你要查询的关键字");
			st=sc.nextLine();
			sql="select * from student where stu_id like '%"+st+"%'";
			break three;
		case 5 :System.out.println("请输入你要查询的关键字");
			st=sc.nextLine();
			sql="select * from student where stu_address like '%"+st+"%'";
			break three;
		case 6:
			sql="select * from student";
			break three;
		case 7:
			break three;
			default:System.out.println("输入有误，请重新输入");
		
		}
		}
		
		
		
		if(sql != null) {
		
		try {
			Class.forName("oracle.jdbc.OracleDriver");
			conn=DriverManager.getConnection(url, user, pass);
			sta=conn.createStatement();
			re=sta.executeQuery(sql);
			while(re.next()) {
				System.out.println("编号:"+ re.getObject(1)+" 姓名:"+re.getObject(3)+" 性别:"+re.getObject(4)+" 年龄:"+re.getObject(5)+" 电话:"+re.getObject(6)+" 地址:"+re.getObject(7));
				
			}
			
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
		
		
			try {
				if(conn !=null)conn.close();
				if(re !=null) re.close();
				if(sta !=null) sta.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}
		
		}
		
	}
	
	private static void addFriends(String user,Statement sta) {
		//添加好友的实现
		/*
		 * 为每个学生创建一个好友列表，为其保存好友
		 * 实质上是为每个id创建一个文件夹，用来保存好友id，如果需要查询好友信息就通过id去数据库查询相对应的信息即可
		 * */
		
		//为每个id创建一个文件夹，保存到aa\\cc文件夹下，文件名称就为其id号
		
		//判断是否有相对应的文件夹及文件，如果没有就创建
		File fil=new File("aa\\cc");
		if(!fil.isDirectory()) {
			fil.mkdirs();
		}
		String f=user+".txt";
		File fil2=new File(fil,f);
		if(!fil2.isFile()) {
			try {
				fil2.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		ResultSet re=null;
		BufferedWriter bw=null;
		try {
			bw=new BufferedWriter(new FileWriter(fil2));
			Scanner sc=new Scanner(System.in);
			one:while(true) {
			System.out.println("请输入要添加的好友编号");
			int id=sc.nextInt();
			//把该ID按行存储在文件夹中,如果数据库中没有该id则添加失败
			String sql="select * from student where stu_id="+id;
			re=sta.executeQuery(sql);
			if(re.next()==true) {
				System.out.println("添加成功");
				bw.write(Integer.toString(id));
				bw.newLine();
				bw.flush();
				
			}else {
				System.out.println("没有该id对应的好友资料");
			}
			
			System.out.println("继续添加请按1，结束请按任意键");
			int temp=sc.nextInt();
			if(temp != 1) {
				break one;
			}
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				bw.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
	}
	
	private static void myFriends(String user,Statement stat) {
		/*查询我的好友，应该导入相对文件中的数据进行查看
		*如果没有找到文件，则说明好友不存在
		*如果找到了，就把文件中的id编号遍历出来，并在数据库中查询对应的信息，并打印出来
		*
		*/
		//判断是否存在相对应的文件夹
		File fil=new File("aa\\cc");
		String f=user+".txt";
		File fil2=new File(fil,f);
		
		if(fil2.isFile() == true) {
			//获取文件夹中每一行的数据
			BufferedReader br=null;
			ResultSet rs=null;
			try {
				 br=new BufferedReader(new FileReader(fil2));
				
				String str=null;
		
				while((str=br.readLine())!=null) {
					//把数据拿到数据库查询
					String sql="select * from student where stu_id="+str;
					 rs=stat.executeQuery(sql);
					while(rs.next()) {
						System.out.println("编号:"+ rs.getObject(1)+" 姓名:"+rs.getObject(3)+" 性别:"+rs.getObject(4)+" 年龄:"+rs.getObject(5)+" 电话:"+rs.getObject(6)+" 地址:"+rs.getObject(7));
					}
				}
				
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				try {
					rs.close();
					br.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
			
			
			
			
		}else {
			System.out.println("你的好友为空");
		}
		
		
	}
	
	private static void friendInformation(String user,Statement stat) {
		//保存好友信息
		System.out.println("请输入你要保存的文件地址");
		Scanner sc=new Scanner(System.in);
		String ur=sc.next();
		File fil1=new File(ur);
		System.out.println("请你输入要保存文件的文件名");
		String file=sc.next();
		File fil3=new File(fil1,file);
		
		//判断所选文件夹是否存在
		 if(fil1.isDirectory()==false) {
			 fil1.mkdirs();
		 }
		 //判断文件夹是否存在
		 if(fil3.isFile()==false) {
			 try {
				fil3.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		 }
		 //创建输出流
		
		 FileWriter bw2=null;
		File fil=new File("aa\\cc");
		String f=user+".txt";
		File fil2=new File(fil,f);
		
		if(fil2.isFile() == true) {
			//获取文件夹中每一行的数据
			BufferedReader br=null;
			//保存到指定文件
			BufferedWriter bw=null;
			ResultSet rs=null;
			try {
				bw2=new FileWriter(fil3);
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				 br=new BufferedReader(new FileReader(fil2));
				
				String str=null;
		
				while((str=br.readLine())!=null) {
					//把数据拿到数据库查询
					String sql="select * from student where stu_id="+str;
					 rs=stat.executeQuery(sql);
					 while(rs.next()) {
						 String st="编号:"+ rs.getObject(1)+" 姓名:"+rs.getObject(3)+" 性别:"+rs.getObject(4)+" 年龄:"+rs.getObject(5)+" 电话:"+rs.getObject(6)+" 地址:"+rs.getObject(7);
						 //把数据保存到文件夹 
						 bw2.write(st);
						 bw2.flush();
					 }
					
				}
				
				
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}finally {
				try {
					rs.close();
					br.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				
			}
			
			
			
			
		}else {
			System.out.println("你的好友为空");
		}
		
		
	}
	
	
	
	private static void LeavingAMessage() {
		System.out.println("-------------------------------");
		System.out.println("---------欢迎来到聊天室------------");
		socket so=new socket();
		so.ma();
		
		
	}
	
	public static String getuse() {
		return use;
	}

}
